package com.yonyou.pmclouds.siteinspection.process;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemBillStatusConst;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 计算检查项合格率
 *
 * @author th
 * @date 2019/11/7 10:18
 */
@Component
public class CountPassRateProcessor implements SingleProcessor<SiteInspectionVO> {
    @Override
    public void process(SiteInspectionVO vo, SiteInspectionVO orig) throws BusinessException {
        if (vo == null || ArrayUtils.isEmpty(vo.getInspectitems()))
            return;

        InspectItemVO[] itemVOS = vo.getInspectitems();
        // 检查项总数、通过数
        double totalNum = 0, passNum = 0;
        for (InspectItemVO itemVO : itemVOS) {
            if (InspectItemBillStatusConst.BILLSTATUS_NOT_FIT != itemVO.getBillstatus())
                totalNum++;

            if (InspectItemBillStatusConst.BILLSTATUS_PASS == itemVO.getBillstatus())
                passNum++;
        }

        // 计算合格率
        Double passRate = Double.valueOf(0);
        if (totalNum > 0) {
            passRate = BigDecimal.valueOf((passNum / totalNum) * 100).setScale(2,
                    BigDecimal.ROUND_HALF_UP).doubleValue();
        }

        vo.setQualifiedRate(passRate);
    }
}
